package com.dabai.alarmsetting

import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.media.RingtoneManager
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar


class MainActivity : AppCompatActivity() {

    private var mMediaPlayer: MediaPlayer? = null
    lateinit var context : Context

    lateinit var tv1 : TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        context = applicationContext

        tv1 = findViewById(R.id.textView)

        var alarm_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        tv1.text= "uri:$alarm_uri"

    }


    /**
     * 按钮事件
     */
    fun play_alarm_clock(view: View) {
        openMediaPlayerVoice()
    }

    fun noplay_alarm_clock(view: View) {
        closeMediaPlayerVoice()
    }

    fun selectFile(view: View) {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(context)){
                requestWriteSettings()
                Toast.makeText(context,"请授权修改系统设置权限！",Toast.LENGTH_LONG).show()
                return
            }
        }

        //通过系统的文件浏览器选择一个文件
        val intent2 = Intent(Intent.ACTION_OPEN_DOCUMENT)
        //筛选，只显示可以“打开”的结果，如文件(而不是联系人或时区列表)
        intent2.addCategory(Intent.CATEGORY_OPENABLE)
        //过滤只显示图像类型文件
        intent2.type = "audio/*"
        startActivityForResult(intent2, 102)

    }


    private fun requestWriteSettings() {
        val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
        intent.data = Uri.parse("package:$packageName")
        startActivity(intent)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        var uri: Uri? = null

        if (requestCode === 102) {
            if (data != null) {
                uri = data.data
            }
            if (uri != null) {
                try {
                    //拿到uri尝试设置铃声

                    RingtoneManager.setActualDefaultRingtoneUri(
                        context,
                        RingtoneManager.TYPE_ALARM,
                        uri
                    )
                    Toast.makeText(context, "设置成功!", Toast.LENGTH_SHORT).show()

                } catch (e: Exception) {
                    Toast.makeText(context, "设置失败!", Toast.LENGTH_SHORT).show()
                }
            }
        }

    }


    /**
     * 播放铃声和停止播放
     */
    private fun openMediaPlayerVoice() {

        if (mMediaPlayer != null) {
            Snackbar.make(window.decorView, "铃声已经在播放了!", Snackbar.LENGTH_SHORT).show();
            return
        }

        val mediaUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
        mMediaPlayer = MediaPlayer.create(this, mediaUri)
        if (mMediaPlayer != null) {
            mMediaPlayer!!.isLooping = true
            mMediaPlayer!!.start()
        }
    }

    private fun closeMediaPlayerVoice() {

        if (mMediaPlayer == null) {
            Snackbar.make(window.decorView, "铃声已经停止播放了!", Snackbar.LENGTH_SHORT).show();
            return
        }

        if (mMediaPlayer != null) {
            mMediaPlayer!!.stop()
            mMediaPlayer!!.release()
            mMediaPlayer = null
        }
    }




}